package logic

import (
	"context"
	"net/http"
	"path/filepath"
	"time"
	"wechat1/pkg"

	"wechat1/wechat1api/internal/svc"
	"wechat1/wechat1api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UploadImageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUploadImageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadImageLogic {
	return &UploadImageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UploadImageLogic) UploadImage(req *http.Request) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	file, header, err := req.FormFile("image")
	if err != nil {
		return nil, err
	}
	ext := filepath.Ext(header.Filename)

	FileName := time.Now().Format("20060102150405") + ext
	yun, err := pkg.QiNiuYun(file, FileName)
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: "图片上传失败",
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Code:    200,
		Message: "图片上传成功",
		Data:    yun,
	}, nil
}
